package com.itheima.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.ssm.domain.Orders;
import com.itheima.ssm.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private IOrdersService iOrdersService;

    /**
     *@描述      利用PageHenlper分页插件，进行查询全部信息
     *@创建人  luopengchang
     *@创建时间  2019/9/24
     */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(
            @RequestParam(name = "page",required = true,defaultValue = "1")Integer page,
            @RequestParam(name = "size",required = true,defaultValue = "4")Integer size
    ) throws Exception {
        ModelAndView mv = new ModelAndView();
        List <Orders> ordersList = iOrdersService.findAll(page,size);
        //PageInfo就是一个分页的bean , 进行封装分页信息
        PageInfo pageInfo = new PageInfo(ordersList);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("orders-page-list");
        return mv;
    }

    @RequestMapping("/findById.do")
    public ModelAndView findById(
            @RequestParam(name = "id",required = true)Integer ordersId
    )throws Exception{
        ModelAndView mv = new ModelAndView();
        Orders order = iOrdersService.findById(ordersId);
        mv.addObject("orders",order);
        mv.setViewName("orders-show");
        return mv;
    }
}
